<template>
  <div class="container">
    <div class="left">
      <!-- 卡片视图区域 -->
      <el-card>
        <h3>会员管理</h3>
        <!-- 搜索与添加区域 -->
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input placeholder="请输入搜索内容" v-model="val"
              @keyup.enter.native="getAllMembers" clearable>
              <el-button slot="append" icon="el-icon-search"
                @click="getAllMembers"></el-button>
            </el-input>
          </el-col>
        </el-row>

        <!-- 主体 -->
        <el-table :data="vipList" style="width: 100%" border>
          <!-- id -->
          <el-table-column prop="_id" label="会员id">
          </el-table-column>
          <!-- 名称 -->
          <el-table-column prop="nickName" label="用户名">
          </el-table-column>
          <!-- 头像 -->
          <el-table-column prop="avatarUrl" label="头像">
            <template width="95" slot-scope="scope">
              <img style="width: 95px; height: 95px; border: none"
                :src="scope.row.avatarUrl" />
            </template>
          </el-table-column>
          <!-- 住址 -->
          <el-table-column prop="province" label="住址">
          </el-table-column>
          <!-- vip截止时间 -->
          <el-table-column prop="vipdate" label="vip截止时间">
            <template width="90" slot-scope="scope">
              <el-input type="text" v-model="changeData.vipdate"
                v-show="scope.row.isEdit" />
              <span v-show="!scope.row.isEdit">{{ scope.row.vipdate }}</span>
            </template>
          </el-table-column>

          <!-- 操作 -->
          <el-table-column prop="" label="操作" width="300">
            <template slot-scope="scope">
              <!-- 充值 -->
              <el-button type="primary" size="mini" v-show="!scope.row.isEdit"
                @click="change(scope.row._id)">充值会员
              </el-button>
              <el-button size="mini" type="success" v-show="scope.row.isEdit"
                @click="updateMemberToVip"><i
                  class="el-icon-upload el-icon--right"></i>保存</el-button>
              <!-- 取消会员 -->
              <el-button size="mini" type="warning"
                @click="cancelMember(scope.row._id)">取消会员</el-button>
              <!-- 删除 -->
              <el-button size="mini" type="danger" icon="el-icon-delete"
                @click="delMember(scope.row._id)">删除</el-button>

            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      vipList: [],
      changeData: {},
      changeNum: 0,
      queryInfo: { pageSize: 5, currentPage: 1 },
      val: "",
      visible: false,
    };
  },
  methods: {
    // 获取列表
    async getAllMembers() {
      // 发请求
      let { data } = await this.$api.vipApi.getAllMembers();
      let arr = data.data

      arr.forEach((v) => {
        v.isEdit = false;
      });
      this.vipList = arr
      console.log(data);
    },
    //删除
    delMember(_id) {
      this.$api.vipApi.delMember({ _id }).then(({ data }) => {
        console.log(data);
        if (data.code == 405) {
          this.$message.error(data.error);
          return
        }
        // 删除提示
        this.$message.success(data.message);
        this.getAllMembers();

      });
    },
    // 取消会员
    cancelMember(_id) {
      this.$api.vipApi.cancelMember({ _id }).then(({ data }) => {
        this.$message.success(data.meta.msg)
        this.getAllMembers();
        // console.log(data.meta);
      })
    },
    // 修改
    change(id) {
      if (this.changeNum) return this.$message.error("请先完成当前修改");
      this.changeNum = 1;
      let res = this.vipList.filter((v) => v._id == id)[0];
      res.isEdit = true;
      this.changeData = { _id: res._id, vipdate: res.vipdate };
      // console.log(this.changeData);
    },
    // 充值会员
    updateMemberToVip() {
      console.log({ _id: this.changeData._id, date: this.changeData.vipdate });
      this.$api.vipApi
        .updateMemberToVip({ _id: this.changeData._id, date: this.changeData.vipdate })
        .then(({ data }) => {
          console.log(data);
          this.$message.success(data.meta.msg);
          this.getAllMembers();
          this.changeNum = 0;
        });
    }

  },
  created() {
    this.getAllMembers();
  },
  watch: {
    queryInfo: {
      handler() {
        this.getAllMembers();
      },
      deep: true
    }
  }
};
</script>

<style lang="scss" scoped>
img {
  width: 50px;
  height: 50px;
}

input {
  width: 100px;
}
.container {
  display: flex;
  .left {
    width: 100%;
  }
}
.btn1 {
  margin-right: 10px;
}
</style>
